package org.tio.sitexxx.im.server.handler;

import java.util.Objects;

import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.P2PChatNtf;
import org.tio.sitexxx.im.common.bs.P2PChatNtf.ChatItem;
import org.tio.sitexxx.im.common.bs.P2PChatReq;
import org.tio.sitexxx.im.server.Ims;
import org.tio.sitexxx.im.server.utils.ImUtils;
import org.tio.sitexxx.service.model.main.ChatRecord;
import org.tio.sitexxx.service.model.main.ChatRecordP2p;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.base.UserRoleService;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.service.stat.ChatRecordP2PService;
import org.tio.sitexxx.service.vo.SimpleUser;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.json.Json;

/**
 * 私聊处理者
 * @author tanyaowu 
 * 2016年9月13日 上午10:16:19
 */
@CommandHandler(Command.P2P_CHAT_REQ)
public class P2PChatReqHandler implements ImServerHandler {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(P2PChatReqHandler.class);

	public static final P2PChatReqHandler me = new P2PChatReqHandler();

	public P2PChatReqHandler() {
	}

	@Override
	public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket) throws Exception {
		SimpleUser fromUser = ImUtils.getSimpleUser(channelContext);
		if (fromUser == null) {
			ImUtils.pleaseLogin(channelContext, "登录才能聊天哦");
			return;
		}

		P2PChatReq p2pChatReq = Json.toBean(packet.getBodyStr(), P2PChatReq.class);
		Integer toUserid = p2pChatReq.getTo(); //发给谁

		String c = p2pChatReq.getC(); //聊天内容
		System.out.println("聊天内容-------》"+c);
		if (StrUtil.isBlank(c)) {
			return;
		}

		String displayContent = c;//SensitiveWordsService.findAndReplace(c); //过滤敏感词后的聊天内容
		displayContent = StringEscapeUtils.escapeHtml4(displayContent);

		User toUser = UserService.ME.getById(toUserid);//查询接收方信息
		if (toUser == null) {
			Tio.remove(channelContext, "提供了错误的userid[" + toUserid + "]");
			return;
		} else {
			if (!UserRoleService.checkUserStatus(toUser)) {
				Tio.remove(channelContext, "提供了错误的userid[" + toUserid + "]");
				return;
			}
		}

		SimpleUser to = SimpleUser.fromUser(toUser);

		Byte contenttype = ChatRecord.ContentType.TEXT;
		p2pChatAndLog(channelContext, c, displayContent, fromUser, to, contenttype);
	}

	/**
	 * 私聊，并保存日志
	 * @param channelContext
	 * @param initContent
	 * @param displayContent
	 * @param fromUser
	 * @param to
	 * @param groupid
	 * @param contenttype
	 */
	public static void p2pChatAndLog(ChannelContext channelContext, String initContent, String displayContent, SimpleUser fromUser, SimpleUser to, Byte contenttype) {
		String sessionid = ImUtils.getToken(channelContext);
		String fromIp = channelContext.getClientNode().getIp();
		//		String channelContextId = channelContext.getId();

		ChatRecordP2p chatRecordP2p = ChatRecordP2PService.me.saveChatRecord(sessionid, initContent, displayContent, fromUser.getI(), fromIp, to.getI(), contenttype);

		//		P2PChatNtf p2PChatNtf = new P2PChatNtf();
		//		p2PChatNtf.setF(fromUser);
		//		p2PChatNtf.setTo(to);
		//		p2PChatNtf.setC(displayContent);
		//		p2PChatNtf.setMid(String.valueOf(chatRecord.getId()));

		ChatItem chatItem = P2PChatNtf.ChatItem.fromChatRecord(chatRecordP2p);
		P2PChatNtf p2PChatNtf = new P2PChatNtf();
		p2PChatNtf.setTo(to);
		p2PChatNtf.setFrom(fromUser);
		p2PChatNtf.setItems(new ChatItem[] { chatItem });
		p2PChatNtf.setMode((byte) 2);

		//		ImPacket imPacket = new ImPacket(Command.P2P_CHAT_NTY, p2PChatNtf);
		System.out.println("P2PChatReq:"+Json.toJson(p2PChatNtf));
		ImPacket imPacket = new ImPacket(Command.P2P_CHAT_NTY, p2PChatNtf);
		Ims.sendToUser(to.getI(), imPacket); //发给消息接收者
		if (!Objects.equals(to.getI(), fromUser.getI())) {
			Ims.sendToUser(fromUser.getI(), imPacket); //发给消息发送者
		}
	}

}
